package vim.meet.javassist;

import cn.freemethod.bean.WriteReadBusiness;
import com.sun.java.accessibility.util.Translator;

public class MyTranslator implements Translator {

    @Override
    public void onLoad(ClassPool pool, String className) throws NotFoundException, CannotCompileException {
        if(!className.endsWith("WriteReadBusiness"))
            return;
        CtClass ctClass = pool.getCtClass(className);
        CtMethod ctMethod = ctClass.getDeclaredMethod("writeSomething");
        ctMethod.insertBefore("System.out.println(\"javassist日志\");");
        ctMethod = ctClass.getDeclaredMethod("readSomething");
        ctMethod.insertBefore("System.out.println(\"javassist日志\");");

    }

    @Override
    public void start(ClassPool arg0) throws NotFoundException, CannotCompileException {
        System.out.println("in translator start...");
    }
    
    public static void main(String[] args) {
        WriteReadBusiness business = new WriteReadBusiness();
        String result = business.readSomething();
        System.out.println(result);
        business.writeSomething("write");
    }

}
